var app = angular.module("application", ['application.services','ngAnimate','ngRoute','angular-loading-bar']);

app.config(function($routeProvider, $locationProvider) {

	$routeProvider.when('/home', {
		templateUrl: base_url + "/index.php" + "/app/home",
		controller: "homeCtrl"
	})
	.when('/search', {
		templateUrl: base_url + "/index.php" + "/app/search",
		controller: "searchCtrl"
	})
	.when('/lesson/:lessonId', {
		templateUrl: base_url + "/index.php" + "/app/lesson",
		controller: "lessonCtrl"
	})
	.when('/groups/:groupId', {
		templateUrl: base_url + "/index.php" + "/app/groups"
	})
	.when('/timeline/:region', {
		templateUrl: base_url + "/index.php" + "/app/timeline",
		controller: "timelineCtrl"
	})
	.when('/timeline', { 
		redirectTo: '/timeline/vn' 
	})
	.when('/event/:id', {
		templateUrl: base_url + "/index.php" + "/app/event",
		controller: "eventCtrl"
	})
	.when('/category/:type', {
		templateUrl: base_url + "index.php" + "/app/category",
		controller: "categoryCtrl"
	})
	.when('/category', { 
		redirectTo: '/category/vn' 
	})
	.when('/cat/:catid', {
		templateUrl: base_url + "index.php" + "/app/cat",
		controller: "catCtrl"
	})
	.when('/videos', {
		templateUrl: base_url + "index.php" + "/app/videos",
		controller: "videoCtrl"
	})
	.when('/class/:classId', {
		templateUrl: base_url + "index.php" + "/app/classes",
		controller: "classCtrl"
	})	
	.when('/objtest/', {
		templateUrl: base_url + "index.php" + "/app/choose_test",
		controller: "chooseCtrl"
	})
	.when('/hsplace/', {
		templateUrl: base_url + "index.php" + "/app/history_place",
		controller: "hsplaceCtrl"
	})
	.when('/hsperson/', {
		templateUrl: base_url + "index.php" + "/app/history_person",
		controller: "hspersonCtrl"
	})
	.when('/question/', {
		templateUrl: base_url + "index.php" + "/app/question",
		controller: "questionCtrl"
	})	
	.when('/question/:qsId', {
		templateUrl: base_url + "index.php" + "/app/reference",
		controller: "questionDtCtrl"
	})
	.when('/reference/:refId', {
		templateUrl: base_url + "index.php" + "/app/reference",
		controller: "referenceCtrl"
	})	
	.when('/search/:str', {
		templateUrl: base_url + "index.php" + "/app/search",
		controller: "searchCtrl"
	})	
	.when('/user/', {
		templateUrl: base_url + "index.php" + "/app/user",
		controller: "userCtrl"
	})
	.when('/user/:action', {
		templateUrl: base_url + "index.php" + "/app/user_action",
		controller: "userActCtrl"
	})	
	.when('/about', {
		templateUrl: base_url + "index.php" + "/app/about",
		controller: "simplest"
	})
	.otherwise({ redirectTo: '/home' });

	// $locationProvider.html5Mode(true);
});

app.value("globalVariable", {
	"searchKeyword": "Đang tìm kiếm"
});
app.constant('BASE_URL', base_url);

// app.factory('searchCache', function($cacheFactory) {
// 	return $cacheFactory('searchData');
// });

function GlobalController($scope, $http, $location, $rootScope, BASE_URL, globalService) {
	$scope.loadingView = true;
	$scope.isLogin = false;
	$scope.loginF = {userName: "", password: ""};
	$scope.newuser = {email: "khoi@gmail", userName: "khoing", password: "123", repassword:"123"};

	$scope.headSearch = "";

	// Functions
	$scope.redirect = function(view) {
		$location.path('/' + view);
	}

	$scope.regisAct = function() {
		if($scope.newuser.password != $scope.newuser.repassword) {
			getAlert('Nhập lại mật khẩu không trùng khớp!');
			return;
		}		
		globalService.regisAct($scope.newuser).then(function(msg) {
			switch(msg) {
				case 'nomatch':
					getAlert('Nhập lại mật khẩu không trùng khớp!');
					break; 
				case 'exists':
					getAlert('Tên đăng nhập đã tồn tại, vui lòng chọn tên khác');
					break;
				case 'success':
					getAlert('Đăng ký thành công');
					$scope.newuser = {email: "", userName: "", password: "", repassword:""};
					$('#registerModal').modal('hide');
					break;
				default: getAlert('Đã xảy ra lỗi, vui lòng thực hiện lại');
			}
		});
	}

	$scope.loginAct = function() {
		user = {
			username: $scope.loginF.userName,
			password: $scope.loginF.password
		};
		$http({
			url: BASE_URL + 'user/getlogin',
			method: "POST",
			data: $.param({'user': user}),
			headers: {'Content-Type': 'application/x-www-form-urlencoded'}
		}).then(function(promise) {
			if(promise.data == "Đăng nhập thành công") {
				$scope.isLogin = true;
				$("#loginModal").modal('hide');
				getAlert("Đăng nhập thành công");
			}
			else {
				getAlert('Tên đăng nhập hoặc mật khẩu không đúng');
			}
		})
	}

	$scope.logoutAct = function() {
		$http.post(BASE_URL + 'user/getLogout').then(function(promise) {
			$scope.isLogin = false;
		})
	}

	$scope.getSearch = function() {
		str = $scope.headSearch;
		if(str == "") {
			getAlert('Vui lòng nhập từ khóa trước khi tìm kiếm');
			return;
		}
		$location.path('/search/' + str);
		$('#searchModal').modal('hide');
	}

	// =============================================================================
	// =============================================================================

	$scope.$on('$routeChangeStart', function() {
		$scope.loadingView = true;
		$('body').removeClass('auto-scroll');

		$http.post(BASE_URL + 'user/check_session').then(function(promise) {
			if(promise.data != '901nosession') {
				$scope.isLogin = true;
			}
			else $scope.isLogin = false;
		})


	});


	// =============================================================================
	// ==================GLOBAL=FUNCTION============================================
	getAlert = function(str) {
		$("#alertMessage .msg-content").html(str);
		$('#alertMessage').modal('show');
	}
}

app.run(function($rootScope) {

	$rootScope.$on('$routeChangeSuccess', function() {
		setTimeout(function() {
			$('.main-menu').removeClass('show');
			$('.main-page').removeClass('hide-left');
			$('#topbar').removeClass('hide-left');

			fullPageEffect();
		}, 200);
	});
}); 

app.controller('simplest', function($scope) {
	setTimeout(function() {
		$scope.$apply(function() {
			$scope.$parent.loadingView = false;
		})
	},1000);
})